# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render
from django.http import HttpResponse
from . import models


# Create your views here.
# #返回内容
# def index(request):
# 	#return HttpResponse('Hello,world!')
# 	return render(request, 'blog/index.html')

#显示列表信息
def index(request):
	#return HttpResponse('Hello,world!')
	#article =models.Article.objects.get(pk=1) #显示单条信息
	#return render(request, 'blog/index.html',{'article':article})
	articles = models.Article.objects.all()
	return render(request, 'blog/index.html',{'articles':articles})

#文章id
def article_page(request,article_id):
    article =models.Article.objects.get(pk=article_id)
    return render(request, 'blog/article_page.html',{'article':article})


def edit_page(request,article_id):
	if str(article_id) == '0':
		return render(request,'blog/edit_page.html')
	article =models.Article.objects.get(pk=article_id)
	return render(request,'blog/edit_page.html',{'article':article})


def edit_action(request):
	title   = request.POST.get('title','TITLE')
	content = request.POST.get('content','CONTENT')
	article_id = request.POST.get('article_id','0')
	if article_id == '0':
		articles = models.Article.objects.all()
		models.Article.objects.create(title=title, content=content)
		return render(request, 'blog/index.html',{'articles':articles})
	article =models.Article.objects.get(pk=article_id)
	article.title = title
	article.content = content
	article.save()
	return render(request, 'blog/article_page.html',{'article': article})